package com.guan.ioc;

import com.guan.ioc.component.UserComponent;
import com.guan.ioc.config.UserConfig;
import com.guan.ioc.config.UserInfoConfig;
import com.guan.ioc.controller.UserController;
import com.guan.ioc.controller.UserController2;
import com.guan.ioc.controller.UserController3;
import com.guan.ioc.model.UserInfo;
import com.guan.ioc.repository.UserRepository;
import com.guan.ioc.service.UserService;
import org.apache.naming.factory.DataSourceLinkFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

//@ComponentScan(basePackages = "com.guan.ioc")
@SpringBootApplication
public class SpringIocDemoApplication {
    public static void main(String[] args) {
        // 1. 通过类名获取对象
        ApplicationContext context = SpringApplication.run(SpringIocDemoApplication.class, args);
//        UserController bean = context.getBean(UserController.class);
//        System.out.println(bean);
//        bean.sayHi();
//
//        // 2. 通过BeanName获得对象
//        UserController userController = (UserController)context.getBean("userController");
//        System.out.println(userController);
//        userController.sayHi();

        // 构造方法注入
//        UserController2 userController2 = (UserController2)context.getBean("userController2");
//        System.out.println(userController2);
//        userController2.sayHi();

//        // Setter 注入
//        UserController3 userController3 = (UserController3)context.getBean("userController3");
//        userController3.sayHi();
//
//        // 3. 通过类名加类型获取对象
//        UserController userController1 = context.getBean("userController", UserController.class);
//        System.out.println(userController1);
//        userController1.sayHi();
//
//        System.out.println("=======================");
//
//        UserService bean1 = context.getBean(UserService.class);
//        bean1.doService();
//
//        UserRepository bean = context.getBean("ur", UserRepository.class);
//        bean.doRepository();
//
//        UserComponent bean1 = context.getBean(UserComponent.class);
//        bean1.doComponent();
//
//        UserConfig bean2 = context.getBean(UserConfig.class);
//        bean2.doConfig();
//        DataSourceLinkFactory bean = context.getBean(DataSourceLinkFactory.class);
//        System.out.println(bean);
//
//        UserInfo bean = context.getBean("zhangsan",UserInfo.class);
//        System.out.println(bean);
//
//        UserInfo bean1 = context.getBean("user1",UserInfo.class);
//        System.out.println(bean1);
//
//        UserInfo bean2 = context.getBean("user2",UserInfo.class);
//        System.out.println(bean2);
    }
}
